package com.example.luke.cashorderingsystem.Common;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.example.luke.cashorderingsystem.Util.GlobalFunction;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by luke on 11/24/16.
 */

public class Setting  {
   private String devID="1";


    private final String devIdKey="devIdKey";
    private final String devIdListKey="devIdListKey";
    private static Setting setting;
    private  ArrayList<String> devIdList;
    private Context context;

    public  static Setting getInstance(){

        if (setting==null){
            setting=new Setting();

        }

        return setting;

    }



    public void init(Context context){
        SharedPreferences preferences=context.getSharedPreferences("setting",Context.MODE_PRIVATE);
        this.context=context;

        devID=preferences.getString(devIdKey,"1");

        Set<String> set=preferences.getStringSet(devIdListKey,null);
        if (set!=null){

           devIdList=new ArrayList<>();
            for (int i = 0; i <set.size() ; i++) {
                devIdList.add((i+1)+"");
            }

        }else {
            devIdList=new ArrayList<>();
            devIdList.add("1");

        }



    }



    public  void saveDevID(){

        SharedPreferences preferences=context.getSharedPreferences("setting",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString(devIdKey,devID).apply();


    }
    public void saveDevIdList(){

        Set<String>temp=new HashSet<>();
        temp.addAll(devIdList);

        SharedPreferences preferences=context.getSharedPreferences("setting",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putStringSet(devIdListKey,temp).apply();

    }

    public ArrayList<String> getDevIdList() {
        return devIdList;
    }

    public String getDevID() {
        return devID;
    }

    public void setDevID(String devID) {
        this.devID = devID;

    }
}
